package com.example.egweibo1.controller;

import com.example.egweibo1.base.response.JsonResult;
import com.example.egweibo1.base.response.Status;
import com.example.egweibo1.mapper.CommentMapper;
import com.example.egweibo1.pojo.dto.CommentParam;
import com.example.egweibo1.pojo.entity.Comment;
import com.example.egweibo1.pojo.vo.CommentVO;
import com.example.egweibo1.pojo.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/comment")
@Api(tags = "评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("/insert")
    @ApiOperation("发布评论")
    @ApiOperationSupport(order = 10)
    @Validated
    public JsonResult insertComment(@RequestBody CommentParam commentParam, @ApiIgnore HttpSession session){
        UserVo userVo=(UserVo)session.getAttribute("user");
        if(userVo==null){
            return new JsonResult(Status.NO_LOGIN,"用户未登录");
        }
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentParam,comment);
        comment.setUserId(userVo.getId());
        comment.setCreateTime(new Date());
        commentMapper.insertComment(comment);
        return JsonResult.ok();

    }
    @ApiOperation("获取评论列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("/selectByWeiboId")
    @ApiImplicitParam(name = "weiboId",value = "微博id",required = true,type="Long")
    public JsonResult getCommentList(@NonNull Long weiboId){
        List<CommentVO> commentVOList=commentMapper.selectByWeiboId(weiboId);
        return JsonResult.ok(commentVOList);
    }
}
